from py_stealth import *

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def SetX(self, x):
        self.x = x

    def SetY(self, y):
        self.y = y


"""
    以下问题根据需要进行修改
"""

# 使用的武士道技能
_bushidoSkill = 'Confidence'
# 使用的特攻技能，现在的版本有BUG不能使用，先不用管
_useAbility = 'Whirlwind Attack'
# 使用的特攻，True为主特攻，False为副特攻
_usePrimaryAbility = False
# 要拾取的材料，我没有填，这个得自己填
materials = [0x0EED]
# 是否拾取尸体
_isLoop = False
# 使用的武器类型，TRUE为斧头，False为镰刀
_useAxe = True

"""
    以下内容为程序体，请不要修改
"""

def GetNearEnemyCount():
    enemy_list = []
    SetFindDistance(1)
    FindType(0xFFFFFFFF, Ground())
    for found in GetFindedList():
        if GetNotoriety(found) > 2:
            enemy_list.append(found)
    return len(enemy_list)


def GetEnemy():
    target_id = None
    target_distance = 0
    SetFindDistance(10)
    FindType(0xFFFFFFFF, Ground())
    for found in GetFindedList():
        if GetNotoriety(found) > 2:
                if target_id:
                    found_distance = abs(GetX(found) - GetX(Self())) + abs(GetY(found) - GetY(Self()))
                    if found_distance < target_distance:
                        target_id = found
                        target_distance = found_distance
                else:
                   target_id = found
                   target_distance = abs(GetX(found) - GetX(Self())) + abs(GetY(found) - GetY(Self()))
    if target_id:
        AddToSystemJournal(GetName(target_id)+' '+str(target_distance))
    return target_id


def MoveToTarget(target):
    if abs(GetX(target) - GetX(Self())) > 0 or abs(GetY(target) - GetY(Self())) > 0:
        NewMoveXY(GetX(target), GetY(target), False, 100, True)


def CheckBuff():
    if not IsActiveSpellAbility(_bushidoSkill):
        Cast(_bushidoSkill)
        CheckLag(1000)
    if not IsActiveSpellAbility(_useAbility):
        if _usePrimaryAbility:
            UsePrimaryAbility()
        else:
            UseSecondaryAbility()
        CheckLag(1000)


def AttackEnemy():
    target = GetEnemy()
    #AddToSystemJournal('AttackEnemy:' + GetTooltip(target))
    if target:
        MoveToTarget(target)
        Attack(target)
    Wait(600)

def LoopCorpse():
    if not _isLoop:
        return
    SetFindDistance(22)
    while FindType(0x2006, Ground()) > 0:
        corpses = GetFindedList()
        for corpse in corpses:
            AddToJournal('start loop: '+GetTooltip(corpse))
            # NewMoveXY(GetX(corpse), GetY(corpse), False, 1, True)
            UseObject(corpse)
            Wait(600)
            for material_type in materials:
                if FindType(material_type, corpse) > 0:
                    Grab(FindItem(), FindQuantity())
                    CheckLag(1000)
                    Wait(600)
            Ignore(corpse)
            AddToJournal('loop end')


def CheckWearpon():
    weapon = 0x27AD
    if _useAxe:
        weapon = 0x0F4B
    if ObjAtLayer(LhandLayer()) == 0:
        if FindType(weapon, Backpack()) > 0:
            Equip(RhandLayer(), FindItem())
            Wait(1000)

    """
    if ObjAtLayer(LhandLayer()) == 0:
        if FindType(0x1B73, Backpack()) > 0:
            Equip(LhandLayer(), FindItem())
            Wait(1000)
    """


def CheckLocation(originLocation):
    currentLocation = Point(GetX(Self()), GetY(Self()))
    shiftLocation = abs(originLocation.x - currentLocation.x) + abs(originLocation.y - currentLocation.y)
    if shiftLocation > 40:
        NewMoveXY(originLocation.x, originLocation.y, False, 200, True)

def Main():
    originLocation = Point(GetX(Self()), GetY(Self()))
    while Connected() and not Dead():
        CheckLocation(originLocation)
        CheckWearpon()
        CheckBuff()
        AttackEnemy()
        LoopCorpse()


Main()
